package ru.vsu.amm.fuzzy;

import java.util.List;

import ru.vsu.amm.algebra.Matrix;

/**
 * 
 * @author Иванов Илья
 * @since 21 января 2013
 *
 */
public class FuzzyUtils {
	
	public static <T> boolean checkScoresVectors(List<T[]> scoresVectors) {
		boolean ok = true;
		for (int i = 0; i < scoresVectors.size() - 1; i++) {
			ok = scoresVectors.get(i).length == scoresVectors.get(i + 1).length;
			if (!ok)
				break;
		}
		
		return ok;
	}
	
	public static boolean checkPairedComparisonsMatrix(Matrix matrix) {
		if (matrix == null || !matrix.isSquare() || matrix.rows() == 0)
			return false;
		
		for (int i = 0; i < matrix.rows(); i++)
			if (Double.compare(matrix.get(i, i), 0.0) != 0)
				return false;
		
		for (int i = 0; i < matrix.rows(); i++)
			for (int j = i + 1; j < matrix.columns(); j++) {
				double ij = matrix.get(i, j);
				double ji = matrix.get(j, i);
				if (Double.compare(ij, 1.0) != 0 && Double.compare(ij, 2.0) != 0 ||
					Double.compare(ji, 1.0) != 0 && Double.compare(ji, 2.0) != 0)
					return false;
				
				if (Double.compare(ij, ji) == 0)
					return false;
			}
		
		return true;
	}
	
	public static boolean checkWeightsVector(double[] weights) {
		if (weights == null || weights.length == 0)
			return false;
		
		double sum = 0;
		for (int i = 0; i < weights.length; i++) {
			if (weights[i] < 0)
				return false;
			
			sum += weights[i];
		}
		
		return Double.compare(sum, 1.0) == 0;
	}
}